﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HYBYSoft.Company.Models;

namespace HYBYSoft.Company.WebSite.Areas.Admin.Controllers
{
    public class ExceptionLogController : Controller
    {
        /// <summary>
        /// 数据库实体上下文对象
        /// </summary>
        CompanySiteEntities _dbContext = new CompanySiteEntities();

        /// <summary>
        /// 异常日志Get请求
        /// </summary>
        /// <returns></returns>
        [Authentication]
        public ActionResult Index(string title, DateTime? start, DateTime? end)
        {
            //获得查询条件
            string name = title ?? string.Empty;
            DateTime startTime = start.HasValue ? start.Value : DateTime.MinValue;
            DateTime endTime = end.HasValue ? end.Value : DateTime.MaxValue;

            //查询满足条件的日志信息
            var exceptionLogs = from l in _dbContext.ExceptionLogs
                                where l.ExceptionMessage.Contains(name) && (l.ThrowTime >= startTime && l.ThrowTime <= endTime)
                                orderby l.ThrowTime descending
                                select l;

            return View(exceptionLogs);
        }

        /// <summary>
        /// 日志查询
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Search()
        {


            return RedirectToAction("Index", new { title = Request["txtTitle"], start = Request["startTime"], end = Request["endTime"] });
        }

        /// <summary>
        /// 日志详细查看页面Get请求
        /// </summary>
        /// <param name="logId">异常日志编号</param>
        /// <returns></returns>
        [Authentication]
        public ActionResult Details(int logId,string pageId)
        {
            ViewBag.PageId = pageId;

            var log = _dbContext.ExceptionLogs.FirstOrDefault(e => e.ExceptionID == logId);

            return View(log);
        }
    }
}
